/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.User;
import Model.UserManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author 013v
 */
public class LoginManagerServlet extends HttpServlet {

    HashMap usermap = new HashMap();

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            boolean checker = false;

            User newUser = new User();

            newUser.setUsername(request.getParameter("username"));
            newUser.setPassword(request.getParameter("password"));
            String name = newUser.getUsername();

            checker = new UserManager().checkUser(newUser);

            if (usermap.containsKey(name) && (Integer) usermap.get(name) >= 3) {
                
                if (true) {
                    if (usermap.get(name).equals(5)) {
                        long timeC = new Date().getTime();
                        long timeS = (Long) usermap.get(name.concat("time"));
                        timeC = timeC - timeS;
                        if (timeC < 1800000) {
                            response.sendRedirect("admin/verwalter.jsp?loginMax=true");
                        } else {
                            usermap.remove(name);
                            response.sendRedirect("index.jsp");
                        }
                    } else if (checker != true) {

                        int index = (Integer) usermap.get(name);
                        index = index + 1;
                        if (index < 6) {
                            usermap.put(name, index);
                        } else {
                            long timeS = new Date().getTime();
                            usermap.put(name, index);
                            usermap.put(name.concat("time"), timeS);
                        }
                        response.sendRedirect("admin/verwalter.jsp?attempt=true");
                    } else {
                        HttpSession session = request.getSession();
                        User tempUser = new UserManager().getIDType(newUser);
                        session.setAttribute("username", newUser.getUsername());
                        session.setAttribute("uid", tempUser.getAccountID());
                        session.setAttribute("acctype", tempUser.getUserType());
                        session.setAttribute("status", tempUser.getStatus());
                        usermap.remove(name);
                        if (tempUser.getUserType() == 5) {
                            session.setAttribute("acctype", "admin");
                            response.sendRedirect("admin/admin.jsp");
                        } else {
                            response.sendRedirect("index.jsp");
                        }
                    }
                } else {
                    System.out.println("a123sd");
                    int index = (Integer) usermap.get(name);
                    index = index + 1;
                    if (index < 5) {
                        System.out.println("asd");
                        usermap.put(name, index);
                    } else {
                        System.out.println("as1d");
                        long timeS = new Date().getTime();
                        usermap.put(name, index);
                        usermap.put(name.concat("time"), timeS);
                    }
                    System.out.println("as13d");
                    response.sendRedirect("admin/verwalter.jsp?attempt=true");
                }
            }

            if (usermap.containsKey(name) && (Integer) usermap.get(name) < 2) {
                if (checker != true) {
                    int index = (Integer) usermap.get(name);
                    index = index + 1;
                    if (index < 5) {
                        usermap.put(name, index);
                    } else {
                        long timeS = new Date().getTime();
                        usermap.put(name, index);
                        usermap.put(name.concat("time"), timeS);
                    }
                    response.sendRedirect("admin/verwalter.jsp?loginAttempt=false");
                } else {
                    HttpSession session = request.getSession();
                    User tempUser = new UserManager().getIDType(newUser);
                    session.setAttribute("username", newUser.getUsername());
                    session.setAttribute("uid", tempUser.getAccountID());
                    session.setAttribute("acctype", tempUser.getUserType());
                    session.setAttribute("status", tempUser.getStatus());
                    usermap.remove(name);
                    if (tempUser.getUserType() == 5) {
                        session.setAttribute("acctype", "admin");
                        response.sendRedirect("admin/admin.jsp");
                    } else //add another if statement depending on role
                    {
                        response.sendRedirect("admin/verwalter.jsp?loginAttempt=false");
                    }

                }
            } else if (checker != true) {
                usermap.put(name, 1);
                response.sendRedirect("admin/verwalter.jsp?loginAttempt=false");
            } else {
                HttpSession session = request.getSession();
                User tempUser = new UserManager().getIDType(newUser);
                session.setAttribute("username", newUser.getUsername());
                session.setAttribute("uid", tempUser.getAccountID());
                session.setAttribute("acctype", tempUser.getUserType());
                session.setAttribute("status", tempUser.getStatus());
                if (tempUser.getUserType() == 5) {
                    session.setAttribute("acctype", "admin");
                    response.sendRedirect("admin/admin.jsp");
                } else //add another if statement depending on role
                {
                    response.sendRedirect("admin/verwalter.jsp?loginAttempt=false");
                }

            }

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
